/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.iti.jets.gpts.service;


import com.iti.jets.gpts.home.PointHome;

import com.iti.jets.gpts.home.TranServiceHome;
import com.iti.jets.gpts.pojo.Point;
import com.iti.jets.gpts.pojo.TranService;
import java.io.Serializable;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.hibernate.Hibernate;
import util.HibernateUtil;

/**
 *
 * @author khalifaabdelaziz
 */
@Path("/transervice")
public class TranServiceService implements Serializable {
    
        TranServiceHome daotranservice = new TranServiceHome(util.HibernateUtil.getSessionFactory());
    @Path("/getalltranservice")
    @Produces(MediaType.APPLICATION_JSON)
    @GET
    public JSONArray getAllTranService() throws JSONException  {
          TranServiceHome tsh = new TranServiceHome(HibernateUtil.getSessionFactory()); 
          PointHome pointHome = new PointHome(HibernateUtil.getSessionFactory());
          
          List<TranService> loadAll = tsh.loadAll();
          
          JSONArray jsontranservces = new JSONArray();
          
          for (TranService tranService : loadAll) {
              JSONObject jsontranservce = new JSONObject();
              Hibernate.initialize(tranService);
              Hibernate.initialize(tranService.getTerminalByTerminal1());
              Hibernate.initialize(tranService.getTerminalByTerminal2());
            //  Hibernate.initialize(tranService.getTranServiceType());
              jsontranservce.put("id", tranService.getId());
              jsontranservce.put("name", tranService.getName());
              //get t1 
              System.out.println(tranService.getTerminalByTerminal1().getIdPoint());
              Point point1 = pointHome.read(tranService.getTerminalByTerminal1().getIdPoint());
              //point one to json
              JSONObject jsonpoint1 = new JSONObject();
              jsonpoint1.put("id", point1.getId());
              jsonpoint1.put("name", point1.getName());
              jsonpoint1.put("location", point1.getLocation());
              jsonpoint1.put("stop", point1.getIsStop());
              //put json point1 in transervice json
              jsontranservce.put("t1", jsonpoint1);
               //get t1 
              Point point2 = pointHome.read(tranService.getTerminalByTerminal2().getIdPoint());
              //point one to json
              JSONObject jsonpoint2 = new JSONObject();
              jsonpoint2.put("id", point2.getId());
              jsonpoint2.put("name", point2.getName());
              jsonpoint2.put("location", point2.getLocation());
              jsonpoint2.put("stop", point2.getIsStop());
              //put json point1 in transervice json
              jsontranservce.put("t2", jsonpoint2);
               JSONObject tst = new JSONObject();
              tst.put("id", tranService.getTranServiceType().getId());
              tst.put("name", tranService.getTranServiceType().getName());
              jsontranservce.put("tst", tst);
              jsontranservce.put("comments", tranService.getCommentses().size());
              jsontranservce.put("feedback", tranService.getFeedbacks().size()); 
              
             jsontranservces.put(jsontranservce);
        }
 
        return jsontranservces;
    }
    //get all information about Specific transervice name(176)

    public TranService getInfo(String name) {

        return daotranservice.getObject(name);
    }





}
